import XmlStream from '../../../utils/xml-stream';
import BaseXform from '../base-xform';
import CommentXform from './comment-xform';


class CommentsXform extends BaseXform {
    constructor() {
        super();
        this.map = {
            comment: new CommentXform(),
        };

    }

    render(xmlStream, model) {
        model = model || this.model;
        xmlStream.openXml(XmlStream.StdDocAttributes);
        xmlStream.openNode('comments', CommentsXform.COMMENTS_ATTRIBUTES);

        // authors
        // TODO: support authors properly
        xmlStream.openNode('authors');
        xmlStream.leafNode('author', null, 'Author');
        xmlStream.closeNode();

        // comments
        xmlStream.openNode('commentList');
        model.comments.forEach(comment => {
            this.map.comment.render(xmlStream, comment);
        });
        xmlStream.closeNode();
        xmlStream.closeNode();
    }


    parseXmlObject(node) {
        if (node._name === 'commentList') {
            this.model = {
                comments: [],
            };
            node._elements?.forEach(item => {
                this.parser = this.map.comment;
                this.parser.parseXmlObject(item);
                this.model.comments.push(this.parser.model);
                this.parser = undefined;
            })
            return true
        }
        return false
    }

    parseOpen(node) {
        if (this.parser) {
            this.parser.parseOpen(node);
            return true;
        }
        switch (node.name) {
            case 'commentList':
                this.model = {
                    comments: [],
                };
                return true;
            case 'comment':
                this.parser = this.map.comment;
                this.parser.parseOpen(node);
                return true;
            default:
                return false;
        }
    }

    parseText(text) {
        if (this.parser) {
            this.parser.parseText(text);
        }
    }

    parseClose(name) {
        switch (name) {
            case 'commentList':
                return false;
            case 'comment':
                this.model.comments.push(this.parser.model);
                this.parser = undefined;
                return true;
            default:
                if (this.parser) {
                    this.parser.parseClose(name);
                }
                return true;
        }
    }
}

CommentsXform.COMMENTS_ATTRIBUTES = {
    xmlns: 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
}

export default CommentsXform;
